﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MedicalManagementAPP.Utils
{
    static class RecordSetManager
    {
        static public void CreateRecordSetByID(string worker, string patient)
        {
            string sql = "insert into RecordSet(RecordDate,WorkerID,PatientID) values(@date,@worker,@patient)";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@date",DateTime.Now.ToString()),
                new SqlParameter("@worker",worker),
                new SqlParameter("@patient",patient),
            };
            Connection.SqlNoQuery(sql, parameters);
        }

        static public bool UpdateRecordDescribe(string id, string describe)
        {
            string sql = "update RecordSet set Describe=@describe where RecordSetID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@describe",describe),
                new SqlParameter("@id",id)
            };
            return Connection.SqlNoQuery(sql, parameters);
        }

        /// <summary>
        /// 通过RecordSet的ID来获取其Records，内容是m.MedicineID,MName,Price,MUnit,MedicineAmount
        /// </summary>
        /// <param name="id">RecordSet的ID</param>
        /// <returns>m.MedicineID,MName,Price,MUnit,MedicineAmount</returns>
        static public DataTable GetRecordSetByID(string id)
        {
            string sql = "select m.MedicineID,MName,Price,MUnit,MedicineAmount " +
                "from Medicines m,MedicalRecords mr " +
                "where m.MedicineID=mr.MedicineID and RecordSetID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return Connection.SqlQuery(sql, parameters);
        }
        static public bool IsRecordSetExist(string id)
        {
            string sql = "select RecordSetID from RecordSet where RecordSetID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return Connection.SqlQuery(sql, parameters).Rows.Count > 0;
        }

        static public DataTable GetRecentRecord()
        {
            string sql = "select PatientName,RecordSetID,RecordDate from RecordSet R,Patients P where R.PatientID=P.PatientID order by RecordDate desc";
            return Connection.SqlQuery(sql);
        }
        static public DataTable GetRecordSetsForPatient(string id)
        {
            // 病历号，登记日期，负责医生，是否缴费，病情描述
            string sql = "select RecordSetID,RecordDate,WorkerName,Paid,Describe from RecordSet R,Workers W where R.WorkerID=W.WorkerID and PatientID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return Connection.SqlQuery(sql,parameters);
        }
        static public bool PayRecordSet(string id)
        {
            string sql = "update RecordSet set Paid=1 where RecordSetID=@id";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return Connection.SqlNoQuery(sql, parameters);
        }
    }
}
